package top.drinyeit.nongmao.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.adapter.MyOrderAdapter;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.fragment.GoodsSearchResultFragment;
import top.drinyeit.nongmao.fragment.ShopSearchResultFragment;

/**
 * Created by wu on 2018/1/18.
 */

public class SearchResultActivity extends BaseActivity implements View.OnClickListener {
    int type;
    public TextView back;
    public TextView search_text;
    private ViewPager search_ViewPager;
    List<Fragment> list = new ArrayList<>();
    private List<String> mNameList;
    MyOrderAdapter myOrderAdapter;
    private TabLayout mTabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);

        initView();
        initIntent();
        initListener();
        setTitle();
    }

    private void initView() {
        search_text = findViewById(R.id.tv_search_text);
        search_ViewPager = findViewById(R.id.search_ViewPager);
        mTabLayout = findViewById(R.id.mTabLayout);
        back = findViewById(R.id.tv_back);
        initViewPager();
    }

    private void initIntent() {
        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            showToast("异常,请重新操作");
        } else {
            if (bundle.get("type") == null){
                type = 0;
            }else {
                type = bundle.getInt("type");
            }
            search_text.setText(bundle.getString("search_key"));

            search_ViewPager.setCurrentItem(type);

        }

    }

    private void initListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityManager.finishActivity(SearchActivity.class);
                finish();
            }
        });
        search_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(SearchResultActivity.this,SearchActivity.class);
//                startActivity(intent);
                finish();
            }
        });
        search_ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    public void initViewPager() {
        mNameList = new ArrayList<>();
        mNameList.add("商品");
        mNameList.add("店铺");
        list.add(new GoodsSearchResultFragment());
        list.add(new ShopSearchResultFragment());
        myOrderAdapter = new MyOrderAdapter(getSupportFragmentManager(), list, mNameList);
        search_ViewPager.setAdapter(myOrderAdapter);
        mTabLayout.setupWithViewPager(search_ViewPager);
    }

    private void setPosition(int type) {
        this.type = type;
    }
}
